<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\cms\controller\admin;

use app\admin\common\AdminController;

class Article extends AdminController
{
    public function index()
    {
        $article_list = new \app\admin\common\AdminList($this);

        if ($article_list->search_word) {
            $where_list[] = "(id = :search_word OR title LIKE CONCAT('%', :search_word, '%'))";
        }

        $where_sql = isset($where_list) ? ' WHERE ' . implode(' AND ', $where_list) : '';
        $where_bind = $article_list->where_map + array('search_word' => $article_list->search_word);

        $count_sql = 'SELECT COUNT(1) FROM {cms_article}' . $where_sql;
        $article_list->total_rows = intval(helper('admin/sql')->query_value($count_sql, $where_bind));
        $article_list->page_count = intval(ceil($article_list->total_rows / $article_list->page_size));

        $select_sql = 'SELECT * FROM {cms_article}' . $where_sql;
        $article_list->list_data = helper('admin/sql')->query_list($select_sql, $where_bind);

        $this->assign('article_list', $article_list);
        $this->display('admin/article/article_list');
    }

    public function create()
    {
        $article_form = new \app\admin\common\AdminForm($this);
        $article_form->form_data = model('cms/article')->initDefault();

        if (request()->isPost()) {
            $article_form->accept_post();

            if ($article_form->save_model() !== false) {
                $this->success('新增成功。', $this->url('index'));
            }
        }

        $this->assign('article_form', $article_form);
        $this->display('admin/article/article_form');
    }

    public function modify($id)
    {
        $article_form = new \app\admin\common\AdminForm($this);
        $article_form->form_data = model('cms/article')->findOrFail($id);

        if (request()->isPost()) {
            $article_form->accept_post();

            if ($article_form->save_model() !== false) {
                $this->success('修改成功。', $this->url('index'));
            }
        }

        $this->assign('article_form', $article_form);
        $this->display('admin/article/article_form');
    }

    public function bulk_delete($ids = array())
    {
        $article_list = model('cms/article')->where('id', 'in', $ids)->select();

        foreach ($article_list as $article_info) {
            $article_info->delete();
        }

        $this->success('删除成功。', $this->url('index'));
    }
}
